﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SystemTest
{
    class SystemSleepManagement
    {

        //定义API函数
        [DllImport("kernel32.dll")]
        static extern uint SetThreadExecutionState(ExecutionFlag flags);

        #region Flag参数解释

        /*
         *它就一个flags参数，这个参数有三个选项，可以组合使用。主要说明如下：
         *只使用Continuous参数时，则是恢复系统休眠策略。
         *不使用Continuous参数时，实现阻止系统休眠或显示器关闭一次
         *组合使用Continuous参数时，实现阻止系统休眠或显示器关闭至线程终止
         */ 
        [Flags]
        enum ExecutionFlag : uint
        {
            System = 0x00000001,
            Display = 0x00000002,
            Continuous = 0x80000000,
        }
        #endregion
        /// <summary>
        ///阻止系统休眠，直到线程结束恢复休眠策略
        /// </summary>
        /// <param name="includeDisplay">是否阻止关闭显示器</param>
        public static void PreventSleep(bool includeDisplay = true)
        {
            if (includeDisplay)
                SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Display | ExecutionFlag.Continuous);
            else
                SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Continuous);
        }

        /// <summary>
        ///恢复系统休眠策略
        /// </summary>
        public static void RestoreSleep()
        {
            SetThreadExecutionState(ExecutionFlag.Continuous);
        }

        /// <summary>
        ///重置系统休眠计时器
        /// </summary>
        /// <param name="includeDisplay">是否阻止关闭显示器</param>
        public static void ResetSleepTimer(bool includeDisplay = false)
        {
            if (includeDisplay)
                SetThreadExecutionState(ExecutionFlag.System | ExecutionFlag.Display);
            else
                SetThreadExecutionState(ExecutionFlag.System);
        }
    }
}
